<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

use think\facade\Hook;

/**
 * 视频评论回复控制器
 * @package app\video\admin
 */
class Reply extends Admin
{
    /**
     * 首页
     * @author shiqiang
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
		
		// 获取筛选
        $map = $this->getMap();
		
		// 获取筛选
		$order = $this->getOrder();
        if(!$order) $order = 'create_time desc';

		//数据获取
        $data = input();
		$comment_id = isset($data['id']) ? $data['id'] : 0;
		$play_id 	= isset($data['play_id']) ? $data['play_id'] : 0;
		if(isset($data['_select_value'])){
			$play_id = $data['_select_value'];
		}
		
		if($play_id > 0){
			$father_id = model('video/VideoPlay') -> getPlayinfoByPlayid($play_id);
			if($father_id['code'] == 200 && $father_id['data']['play_type'] == 1 && $father_id['data']['father_play_id'] > 0){
				$play_id = $father_id['data']['father_play_id'];
			}
			$where = [];
			foreach($map as $value){
				if($value[0] == 'play_id'){
					$value = ['play_id', '=', $play_id];
				}
				$where[] = $value;
			}
			$map = $where;
		}
		if($comment_id > 0) $map[] = ['comment_id', '=', $comment_id];
		// 数据列表
        $data_list = model('VideoPlayCommentReplay') -> withTrashed() -> where($map) -> order($order) -> paginate();
		$rel_list = [];
        foreach ($data_list as $key => $value) {
			$r_nick_name = model('VideoPlayCommentReplay') -> getUcMember() -> find($value['r_uid']);
			$rel_list[$key] = $value;
			$rel_list[$key]['nick_name'] 	= $value['get_uc_member']['nick_name'];
			$rel_list[$key]['r_nick_name'] 	= $r_nick_name['nick_name'];
			
			if($value['reply_status'] > 1){
				$value['reply_status'] = 1;
			}
        }
		//新增
		$btn_add = [
			'title' => '新增',
			'icon'  => 'fa fa-plus-circle',
			'class' => 'btn btn-primary pop',
			'href'  => url('add', ['play_id' => $play_id, 'comment_id' => $comment_id]),
		];
		
		//新增
		$btn_adds = [
			'title' => '回复',
			'icon'  => 'fa fa-fw fa-bookmark',
			'href'  => url('add', ['play_id' => $play_id, 'comment_id' => $comment_id, 'r_id' => '__id__']),
		];
		
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('评论回复列表') // 设置页面标题
            ->setTableName('video_play_comment_reply') // 设置数据表名
			->setSearch(['uid' => '用户id', 'content' => '回复详情']) // 设置搜索参数
			->addTimeFilter('create_time', '', ['开始时间', '结束时间'])
			->addTopSelect('play_id', '选择视频', model('VideoPlay') -> where(['play_operation_status' => 1, 'play_type' => 1])->column('id,main_title')) // 添加筛选
            ->addColumns([ // 批量添加列
                //['__INDEX__', '序号'],
                ['id', '回复id'],
                ['uid', '用户id'],
                ['nick_name', '用户昵称'],
                ['r_nick_name', '回复的用户'],
                ['content', '回复详情'],
                ['support_times', '点赞数'],
                ['create_time', '创建时间', 'datetime'],
                ['reply_status', '当前状态', 'status', '',[0 => '正常:success',1 => '软删除:danger']],
                ['right_button', '操作', 'btn']
            ])
			->addFilter(['uid']) // 添加筛选
			->addOrder('create_time') // 添加排序
            ->addTopButton('custom', $btn_add, true) // 批量添加顶部按钮
			->addRightButton('custom', $btn_adds, true) // 批量添加右侧按钮
            ->addRightButtons('delete') // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }
	
	public function add($play_id = '', $comment_id = '', $r_id = ''){
		// 保存数据
        if ($this->request->isPost()) {
			$data = $this->request->post();
			$uid 		= $data['uid'];
			$play_id 	= $data['play_id'];
			$comment_id = $data['comment_id'];
			$r_uid 		= $data['r_uid'];
			$content 	= $data['content'];

			if ($uid == '' || $play_id == '' || $comment_id == '' || $content == '') {
                $this->error('数据格式错误');
            }
			$rel = controller('Videologic', 'logic') -> add_comment_reply($uid, $play_id, $comment_id , $r_uid, $content);
			if($rel['code'] == 200){
				$this->success('新增成功', null, '_parent_reload');
			}else{
				$this->error($rel['message']);
			}
		}
		
		$uid = config('background_comment_preset');
		
		//通过回复id获取 视频id 回复id 用户id
		$reply_uid = '';
		if($r_id != ''){
			$reply = model('VideoPlayCommentReplay') -> find($r_id);
			$reply_uid = $reply['uid'];
			$play_id = $reply['play_id'];
			$comment_id = $reply['comment_id'];
		}
		$r_uid = model('VideoPlayCommentReplay') -> where('comment_id', $comment_id) -> column('uid');
		
		// 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
		->setPageTitle('新增回复') // 设置页面标题
		->addSelect('uid', '选择回复用户', '系统默认角色选择',  model('app\user_center\model\user\Member') -> where('uid', 'in', $uid)->column('uid,nick_name'), '')
		->addLinkage('play_id', '选择视频', '',  model('VideoPlay') -> where('play_operation_status', 1)->column('id,main_title'), $play_id , url('get_comment_id'), 'comment_id,r_uid')
		->addLinkage('comment_id', '选择评论', '', model('VideoPlayComment') -> where('play_id', $play_id) -> column('id, content'), $comment_id, url('get_r_uid'), 'r_uid')
		->addSelect('r_uid', '选择需要回复用户', '系统回复某一条评论下的某个人的回复',  model('app\user_center\model\user\Member') -> where('uid', 'in', $r_uid)->column('uid,nick_name'), $reply_uid)
		->addFormItems([ // 批量添加表单项
			['textarea', 'content', '回复详情'],
		])
		->fetch();
	}
	
	public function get_comment_id($play_id = ''){
		$comment = model('VideoPlayComment') -> where('play_id', $play_id)->column('id,content');
		$list = [];
		foreach($comment as $key => $value){
			$list[] = ['key' => $key, 'value' => $value];
		}
		
		$arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $arr['list'] = $list; //数据
        return json($arr);
	}
	
	public function get_r_uid($comment_id = ''){
		$reply = model('VideoPlayCommentReplay') -> where('comment_id', $comment_id)->column('uid');
		$r_uid = model('app\user_center\model\user\Member') -> where('uid', 'in', $reply)->column('uid,nick_name');
		$list = [];
		foreach($r_uid as $key => $value){
			$list[] = ['key' => $key, 'value' => $value];
		}
		
		$arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $arr['list'] = $list; //数据
        return json($arr);
	}
	
	public function delete($ids = []){
        $rel = model('VideoPlayCommentReplay') -> destroy($ids, true);
        if($rel){
            $this -> success('删除成功');
        }else{
            $this -> error('删除失败');
        }
    
    }
}
